<?php
namespace Admin\Controller;
use Think\Controller;
class AdvertiserController extends Controller
{
 
 	public function __construct($value='')
	{
		$this->isAjaxLogin();
	}
	// 广告主列表
	public function advertiserList()
	{
      
		$contact_name=I('post.contact_name','');//联系人名
		$company_name=I('post.company_name','');//公司名
		$business_id=I('post.business_id/d');//所属商务
		$page=I('post.page','');
		$page_size=I('post.page_size','');//显示条数

		if (C('IS_FALSE_DEL')) {
			$where['a.is_delete']='0';	 
		} 

		if(!empty($contact_name)) {
            $where['a.contact_name'] = array('like', '%' . $contact_name . '%');
        }

        if(!empty($company_name)) {
            $where['a.company_name'] = array('like', '%' . $company_name . '%');
        }


        if(!empty($business_id)&&($business_id>-1)) {
            $where['a.business_id'] = $business_id;
        }

        
        
		$count = M('advertiser')
            ->alias('a')
            ->join('left join wm_manage b on a.business_id=b.user_id')
            ->where($where)
            ->count();


		$total =  ceil($count / $page_size);
		$limitStart = ($page-1) * $page_size;

		$data = M('advertiser')
            ->alias('a')
            ->join('wm_manage b on a.business_id=b.user_id')
            ->field('a.id,a.company_name,a.contact_name,a.balance,a.contact_phone,a.account_status')
            ->where($where)
            ->limit($limitStart, $page_size)
            ->order('id desc')
            // ->fetchSql(true)
            ->select();
			$result['data']['total_page']=$count;
			$result['data']['list']=$data;
			
		// if (!empty($data)){
			$result['msg']='查询成功';
			$result['code'] = 200;	 
		// }else{
		// 	$result['msg']='暂无数据';
		// 	$result['code']=500;
		// }

		$this->ajaxReturn($result);	
	}

	// 下拉显示所有商务 v1.1
	public function selectList($value='')
	{
		 
		if (C('IS_FALSE_DEL')) {
			$where['is_delete']='0';	 
		}else{
			$where=[];
		}

		$data=M('manage')->field('user_id,user_name')->where($where)->where(['role_id'=>1])->select();
		// if (!empty($data)) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$data;
			 
		// }else{

		// 	$result['msg']='查询失败';
		// 	$result['code']=501;
		// }
		$this->ajaxReturn($result);
	}


	// 广告主添加/修改
	public function advertiserAdd()
	{
		
        
		$id=I('post.id/d');
		$Advertiser=D('Advertiser');//广告主
		  
	 	$data=$Advertiser->field('id',true)->create(I('post.'));//广告主
		if (!$data) {
			$result['msg']=$Advertiser->getError();
			$result['code']=500;
			$this->ajaxReturn($result);
		}
	 	 
	 	if (empty($id)) {
			$res=$Advertiser->add($data); 	
			if ($res) { 
				$result['msg']='添加成功';		 
				$result['code']=200;		 
			}else{
				$result['msg']='添加失败';	
				$result['code']=501;	
			} 
	 	}else{

			$res=$Advertiser->where(['id'=>$id])->save($data); 
			if ($res) { 
				$result['msg']='修改成功';		 
				$result['code']=200;		 
			}else{
				$result['msg']='修改失败';	
				$result['code']=502;	
			} 
	 	}
			
		 			
		$this->ajaxReturn($result);	

	}



    // 广告主删除
    public function advertiserDelById($value='')
    {
        $id=I('get.advertiser_id/d');

        if (C('IS_FALSE_DEL')) {
        	$where['is_delete']='0';
        }else{
        	$where=[];
        }

        $count=M('advertisement')->where(['advertiser_id'=>$id])->where($where)->count();

        if ($count>0)
        {
        	$result['msg']='广告主关联广告不为空,不能删除';
        	$result['code']=501;
        	$this->ajaxReturn($result);
        }

        $count1=M('invoice_order')->where(['adv_id'=>$id])->count();

        if ($count1>0) {
        	$result['msg']='请先删除广告主发票订单'; 
        	$result['code']=502; 
        	$this->ajaxReturn($result);
        }

        if (!C('IS_FALSE_DEL')) {
            $res=M('advertiser')->where(['id'=>$id])->delete();
        }else{
            $res=M('advertiser')->where(['id'=>$id])->save(['is_delete'=>'1']);
        }

        $msg=$res ? '删除成功' : '删除失败';
        $code=$res ? 200 : 500;
        $this->ajaxReturn(['msg'=>$msg,'code'=>$code]);
    }


      // 广告主修改详情
	public function advertiserInfo()
	{
		 
		 $id=I('get.id/d');//广告主id
			$data=M('advertiser')
			->where(['id'=>$id])
			->find();
			
		 
		 // if (!empty($data)) {
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']=$data;
		 // }else{
		 // 	$result['msg']='查询失败';
			// $result['code']=501;
		 // }

		 $this->ajaxReturn($result);
	}

	// 账户封停/解除
	public function accountChange($value='')
	{
		$adv_id=I('get.adv_id/d');

		$Advertiser=M('advertiser');
		 		 
		$account_status=$Advertiser->where(['id'=>$adv_id])->getField('account_status');
		 
		$status=['0'=>'1','1'=>'0'];
 	
		$res=$Advertiser->where(['id'=>$adv_id])->setField('account_status',$status[$account_status]);

		if ($res) {
			$result['msg']='更改成功';
			$result['code']=200;		 
		}else{
			$result['msg']='更改失败';
			$result['code']=500;
		}

		$this->ajaxReturn($result);	 
	}
 
}